package org.darcy.youchat.activity.me.setting;

import android.content.Intent;
import android.view.MenuItem;
import android.view.View;

import org.darcy.youchat.R;
import org.darcy.youchat.YouChatApp;
import org.darcy.youchat.activity.BaseActivity;
import org.darcy.youchat.activity.login.LoginActivity;
import org.darcy.youchat.databinding.ActivitySettingBinding;
import org.darcy.youchat.nimsdk.NimAccountSDK;
import org.darcy.youchat.view.CustomDialog;

/**
 * 设置界面
 */
public class SettingActivity extends BaseActivity {

    private ActivitySettingBinding binding;
    private final String titleName = "设置";

    Intent intent;

    private View mExitDialogView;
    private CustomDialog mDialog;



    @Override
    public void initView() {
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.mToolbar.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(titleName);
        binding.mToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
    }


    @Override
    public void initListener() {
        //1.新消息
        binding.oivNewMsgNotifySet.setOnClickListener(v -> startActivity(new Intent(this, NewMsgNotifySetActivity.class)));
        //2.免打扰
        binding.oivDontDistorbSet.setOnClickListener(v -> startActivity(new Intent(this, DontDisturbSetActivity.class)));
        //3.聊天
        binding.oivChatSet.setOnClickListener(v -> startActivity(new Intent(this, ChatSetActivity.class)));
        //4.隐私
        binding.oivPrivacySet.setOnClickListener(v -> startActivity(new Intent(this, PrivacySetActivity.class)));
        //5.通用
        binding.oivCommon.setOnClickListener(v -> startActivity(new Intent(this, CommonSetActivity.class)));
        //6.安全
        binding.oivAccountAndSafeSet.setOnClickListener(v -> startActivity(new Intent(this, AccountAndSafeSetActivity.class)));
        //7.关于
        binding.oivAbout.setOnClickListener(v -> startActivity(new Intent(this, AboutActivity.class)));
        //8.退出
        binding.oivExit.setOnClickListener(v -> exit());
    }

    private void exit(){
        if (mExitDialogView == null) {
            mExitDialogView = View.inflate(this, R.layout.dialog_exit, null);
            mDialog = new CustomDialog(this, mExitDialogView, R.style.dialog);
            mDialog.show();

            mExitDialogView.findViewById(R.id.tvExitAccount).setOnClickListener(v -> {
                //退出当前账号
                NimAccountSDK.logout();
                intent = new Intent(SettingActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
                mDialog.dismiss();
            });

            mExitDialogView.findViewById(R.id.tvExitApp).setOnClickListener(v -> {
                //退出app
                YouChatApp.exit();
                mDialog.dismiss();
            });

        } else {
            mDialog.show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

}
